// const domainUrl='https://xcx.idaur.com/'
const domainUrl='http://localhost:8771/'

function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function isEmpty(v){
  if(v!=undefined&&v.length>0){return true;}
}

function login(accessPage) {
  wx.login({
    success: function (res) {
      console.log(res.code);
      var code = res.code; //返回code
      wx.request({
        url: domainUrl + 'getShuihuOpenId?code=' + code + '&accessPage=' + '[shuihuka]'+accessPage,
        data: {},
        header: {
          'content-type': 'json'
        },
        success: function (res) {
          console.log(res.data)
          var openid = res.data.openid; //返回openid
          wx.setStorageSync('my_openid', openid);
        }
      })
    }
  })
}

function showToast(obj) {
  if (obj.context == null || obj.title == null) {
    console.error("必须传入微信小程序页面的 this 对象, 和 toast 的内容");
    return;
  }

  obj.context.setData({
    'show_toast': true,
    'toast_content': obj.title,
  });

  setTimeout(() => {
    obj.context.setData({
      'show_toast': false,
      'toast_content': null,
    });
  }, obj.duration || 5000);
}

module.exports = {
  formatTime: formatTime,
  isEmpty:isEmpty,
  login:login,
  showToast: showToast
}
